<?php

namespace App\Http\Controllers\Cart;

use App\Http\Controllers\Controller;
use App\Model\Cart;
use App\Model\Commodity;
use App\Model\Commodity_color;
use App\Model\Commodity_info;
use App\Model\Commodity_size;
use App\Model\User_address;
use Illuminate\Http\Request;


/**
 * Class CartController
 * @package App\Http\Controllers\Cart
 * @User wuqin
 * @Date 2018/4/11 0011
 * @Time 12:01
 */
class CartController extends Controller
{
    /**
     * 方法 : index
     * @User wuqin
     * @DateTime 2018-04-11 12:01
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $data = array();
        $user = session('user_info');
        if (empty($user))
            return $this->error_url('/', '请重新登录', 3);

        $CommodityModel = new Commodity();
        $colorModel = new Commodity_color();
        $sizeModel = new Commodity_size();
        $infoModel = new Commodity_info();
        $cartModel = new Cart();
        $cartRes = $cartModel->get_cart_by_user_id($user["user_id"]);

        $where = array(
            array("where", "user_id", "=", $user["user_id"])
        );
        session(array("user.cart.num" => $cartModel->getSumByWhere($where)));
//        $cartInfo = array(
//            "price" => 0,
//            "number" => 0,
//        );
        if (empty($cartRes)) {
            $cart = array();
            $cartData = array();
        } else {
            foreach ($cartRes as $key => $value) {
                static $mark = 0;
                static $k = 0;
                $mark = $value["commodity_id"] != $k ? $mark + 1 : $mark;
                $k = $value["commodity_id"];
                $commodityRes = $CommodityModel->get_commodity_by_commidity_id($value["commodity_id"]);
                if (empty($commodityRes) || count($commodityRes) == 0) {
                    unset($cartRes[$key]);
                    continue;
                }
                $value = array_merge($value, reset($commodityRes));
                $value = array_merge($value, $colorModel->get_redis('commodity_color:' . $value["color_id"] . ':commodity:' . $value["commodity_id"]));
                $value = array_merge($value, $sizeModel->get_redis('commodity_size:' . $value["size_id"] . ':commodity:' . $value["commodity_id"]));
                $value = array_merge($value, $infoModel->get_redis("commodity_info:" . $value["info_id"] . ":commodity:" . $value["commodity_id"] . ":color:" . $value["color_id"] . ":size:" . $value["size_id"]));
                unset($value["commodity_desc"]);
                $cart[$mark - 1][] = $value;
                $cartData["cart" . $value["cart_id"]] = array(
                    "cart" => $value["cart_id"],
                    "commodity" => $value["commodity_id"],
                    "info" => $value["info_id"],
                    "color" => $value["color_id"],
                    "size" => $value["size_id"],
                    "number" => $value["number"],
                    "price" => $value["unit_price"],
                    "all_price" => $value["all_price"],
                );
                //                $cartInfo["price"] += $value["all_price"];
                //                $cartInfo["price"] += $value["_price"];
                //                $cartInfo["number"]++;
            }
        }
        $data["user"] = $user;
        $data["cart"] = $cart;
//        $data["cartInfo"] = $cartInfo;
        $data["cartData"] = $cartData;
        return view("Cart.index", $data);
    }

    public function edit()
    {
        $data = array();
        $user = session('user_info');
        if (empty($user))
            return $this->error_url('/', '请重新登录', 3);

        $CommodityModel = new Commodity();
        $colorModel = new Commodity_color();
        $sizeModel = new Commodity_size();
        $infoModel = new Commodity_info();
        $cartModel = new Cart();
        $cartRes = $cartModel->get_cart_by_user_id($user["user_id"]);

        if (empty($cartRes))
            return $this->error_url('/cart', '购物车为空', 3);

        $where = array(
            array("where", "user_id", "=", $user["user_id"])
        );
        session(array("user.cart.num" => $cartModel->getSumByWhere($where)));
//        $cartInfo = array(
//            "price" => 0,
//            "number" => 0,
//        );
        if (empty($cartRes)) {
            $cart = array();
            $cartData = array();
        } else {
            foreach ($cartRes as $key => $value) {
                static $mark = 0;
                static $k = 0;
                $mark = $value["commodity_id"] != $k ? $mark + 1 : $mark;
                $k = $value["commodity_id"];
                $commodityRes = $CommodityModel->get_commodity_by_commidity_id($value["commodity_id"]);
                if (empty($commodityRes) || count($commodityRes) == 0) {
                    unset($cartRes[$key]);
                    continue;
                }
                $value = array_merge($value, reset($commodityRes));
                $value = array_merge($value, $colorModel->get_redis('commodity_color:' . $value["color_id"] . ':commodity:' . $value["commodity_id"]));
                $value = array_merge($value, $sizeModel->get_redis('commodity_size:' . $value["size_id"] . ':commodity:' . $value["commodity_id"]));
                $value = array_merge($value, $infoModel->get_redis("commodity_info:" . $value["info_id"] . ":commodity:" . $value["commodity_id"] . ":color:" . $value["color_id"] . ":size:" . $value["size_id"]));
                unset($value["commodity_desc"]);
                $cart[$mark - 1][] = $value;
                $cartData["cart" . $value["cart_id"]] = array(
                    "cart" => $value["cart_id"],
                    "commodity" => $value["commodity_id"],
                    "info" => $value["info_id"],
                    "color" => $value["color_id"],
                    "size" => $value["size_id"],
                    "number" => $value["number"],
                    "price" => $value["unit_price"],
                    "all_price" => $value["all_price"],
                );
                //                $cartInfo["price"] += $value["all_price"];
                //                $cartInfo["price"] += $value["_price"];
                //                $cartInfo["number"]++;
            }
        }
        if (empty($cart))
            return $this->error_url('/cart', '购物车为空', 3);

        $data["user"] = $user;
        $data["cart"] = $cart;
//        $data["cartInfo"] = $cartInfo;
        $data["cartData"] = $cartData;
        return view("Cart.edit", $data);
    }

    /**
     * 方法 : right
     * @User wuqin
     * @DateTime 2018-04-11 12:01
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function right(Request $request)
    {
        $data = array();
        $user = session('user_info');
        if (empty($user))
            return $this->error_url('/', '请重新登录', 3);
        $cartInfo = session("user.cart.info");
        if (empty($cartInfo))
            return $this->error_url('/', '非法操作', 3);

        $CommodityModel = new Commodity();
        $colorModel = new Commodity_color();
        $sizeModel = new Commodity_size();
        $infoModel = new Commodity_info();
        $cartModel = new Cart();
        $cartRes = $cartModel->get_cart_by_cart_ids($cartInfo);
        $cartInfo = array(
            "price" => 0,
            "number" => 0,
        );
        foreach ($cartRes as $key => $value) {
            static $mark = 0;
            static $k = 0;
            $mark = $value["commodity_id"] != $k ? $mark + 1 : $mark;
            $k = $value["commodity_id"];
            $commodityRes = $CommodityModel->get_commodity_by_commidity_id($value["commodity_id"]);
            if (empty($commodityRes) || count($commodityRes) == 0) {
                unset($cartRes[$key]);
                continue;
            }
            $value = array_merge($value, reset($commodityRes));
            $value = array_merge($value, $colorModel->get_redis('commodity_color:' . $value["color_id"] . ':commodity:' . $value["commodity_id"]));
            $value = array_merge($value, $sizeModel->get_redis('commodity_size:' . $value["size_id"] . ':commodity:' . $value["commodity_id"]));
            $value = array_merge($value, $infoModel->get_redis("commodity_info:" . $value["info_id"] . ":commodity:" . $value["commodity_id"] . ":color:" . $value["color_id"] . ":size:" . $value["size_id"]));
            unset($value["commodity_desc"]);
            $cart[] = $value;

            $cartInfo["price"] += ($value["all_price"]);
            $cartInfo["number"]++;
        }
        $userAddModel = new User_address();
        $data["address"] = $userAddModel->get_default_user_address_by_user_id($user["user_id"]);
        if (empty($data["address"]))
            return $this->error_url("/", "无效地址", 3);
        $data["address"] = reset($data["address"]);
        $data["address"]["phone"] = substr_replace($data["address"]["phone"], "****", 3, 4);
        $data["cart"] = $cart;
        $data["cartInfo"] = $cartInfo;
        $userAddModel->set_array_redis("flush_cart" . $user["user_id"], $cart, 30000);
        return view("Cart.right", $data);
    }
}
